<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
define('CREATE_HTML', true);
if(!ican("syscreatehtml")){
 msg('对不起，你没有权限执行本操作！');
}
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';

if ($f == 'index' ) {
	
	$data = array();
	if (file_exists(BASE_ROOT.'article/')) $data[] = array('article', BASE_ROOT.'article/');
	if (file_exists(BASE_ROOT.'index.html')) $data[] = array('index.html', BASE_ROOT.'index.html');
	if (file_exists(BASE_ROOT.'archive.html')) $data[] = array('archive.html', BASE_ROOT.'archive.html');
	if (file_exists(BASE_ROOT.'tag.html')) $data[] = array('tag.html', BASE_ROOT.'tag.html');
	if (file_exists(BASE_ROOT.'link.html')) $data[] = array('link.html', BASE_ROOT.'link.html');
	
	include admintemplate('createhtml');

} else {
	
	if ( $f == 'index' ) {

	
	} elseif ( $f == 'article' ) {
		$cid=getGP('cid');
		$pagesize=getGP('pagesize');
		$start=getGP('start');
		$end=getGP('end');
		$total=getGP('total');
		$count=getGP('count');

		!$pagesize && $pagesize = 20;
		!$count && $count = 0;
		
		$wheresql = " b.ishide = 0 AND a.ischeck = 1 AND a.status < 3 AND a.password = ''";
		(int)$cid && $wheresql .= " AND a.cid = '$cid'";
		(int)$start && $wheresql .= " AND a.id >= '$start'";
		(int)$end && $wheresql .= " AND a.id <= '$end'";
		if (!$total) {
			$total = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."article a JOIN ".DB_PREFIX."category b ON a.cid = b.id WHERE $wheresql LIMIT $count, $pagesize");
		}
		
		$filedir = BASE_ROOT.'article';
		make_dir($filedir);
		if ( !is_dir($filedir) ) {
			exit('网站根目录下的 article 目录创建失败，请手工创建并将权限设置为777。');
		}
		$file = THEME_ROOT.'theme.tpl.php';
		if ( file_exists($file) ) {
			include_once($file);
		}

		$mk_query = $_G['db']->query("SELECT a.id,a.urlname,a.dateline,b.id AS cid,b.urlname AS catename FROM ".DB_PREFIX."article a JOIN ".DB_PREFIX."category b ON a.cid = b.id WHERE $wheresql LIMIT $count, $pagesize");
		while ( $row = $_G['db']->fetch_array($mk_query) ) {
			!$row['catename'] && $row['catename'] = $row['cid'];
			$htmldir = BASE_ROOT.get_html_dir($row['dateline'],$row['catename']);
			if ( !file_exists($htmldir) ) {
				make_dir($htmldir);
			}
			$filepath = $htmldir.'/'.(empty($row['urlname']) ? $row['id'] : $row['urlname']).'.html';
			$get = array('article', $row['id']);
			require(BASE_ROOT.'include/mod_article.php');
			$contents = ob_get_contents();
			$contents .= "\n".'<script type="text/javascript" src="count.php?aid='.$row['id'].'"></script>';
			obclean();
			file_put_contents($filepath, $contents);
			$contents = '';
			unset($contents,$_ENV['article']);
			$count ++;
		}
		if ($total > 0) {
			$percent = ceil(($count/$total)*100);
		} else {
			$percent = 100;	
		}
		$barlen = $percent * 4;
		$url = "admin.php?m=createhtml&f=article&cid={$cid}&pagesize={$pagesize}&start={$start}&end={$end}&total={$total}&count={$count}";
		if ($total > $count) {
			echo '<script>setTimeout("window.location=\''.$url.'\'",200);</script>
			生成进度：<br /><div style="height:20px;width:400px;border:1px solid #ccc"><div style="background:#f0f0f0;width:'.$barlen.'px"></div></div> '.$percent.'%';
		} else {
			echo '任务完成，共创建文件 <b>'.$total.'</b> 个！';
		}
	
	//更新文章归档、标签列表、友情链接
	} elseif ( $f == 'archive' || $f == 'tag' || $f == 'link' ) {
		
		$get = array($f);
		
		$file = THEME_ROOT.'theme.tpl.php';
		if ( file_exists($file) ) {
			include_once($file);
		}
		require_once(BASE_ROOT.'include/mod_'.$f.'.php');
		$contents = ob_get_contents();
		obclean();
		$filepath = BASE_ROOT.$f.'.html';
		file_put_contents($filepath, $contents);
		$contents = '';
		unset($contents);

		exit('成功更新'.$f.'.html：'.$filepath.'<br /><a href="'.$f.'.html" target="_blank">浏览...</a>');
	
	//页面清理
	} elseif ( $f == 'clear' ) {
	
		$filelist = getGP('filelist','P','array');
		foreach ($filelist as $file) {
			$file = BASE_ROOT.str_replace('..','',$file);
			if ( is_file($file) && in_array($file, array(BASE_ROOT.'index.html', BASE_ROOT.'archive.html', BASE_ROOT.'tag.html' ,BASE_ROOT.'link.html')) ) {
				if ( @unlink($file) ) {
					echo "删除$file -- <font color=\"green\">成功！</font><br />";
				} else {
					echo "删除$file -- <font color=\"red\">失败！</font><br />";
				}
			} elseif ( $file == BASE_ROOT.'article' ) {
				remove_dir($file);
				echo "删除$file -- <font color=\"green\">成功！</font><br />";
			}
		}
	}
}
?>